
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for edit_group_bands.
	''' </summary>
	Public Partial Class edit_group_bands
		Inherits BaseWebPage
		Protected tblTopTable As System.Web.UI.WebControls.Table
		Protected ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
		Protected lblEditGroup As System.Web.UI.WebControls.Label
		Protected grpID As Integer = -1
		Protected id As Integer = -1
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.EDIT_GROUP_BANDS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Request.Params(Constants.GROUP_PARAM) = Nothing OrElse Request.Params(Constants.GROUP_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Dim grpBands As GroupBands = Nothing
			Dim grp As Group = Nothing

			Try
				grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				grpBands = GroupBands.GetGroupBandsByGroupID(grpID)
				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If

				If grpBands = Nothing Then
					grpBands = New GroupBands(grpID)
				Else
					id = grpBands.ID
				End If
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			' check if Logged user is the moderator of the group
			If grp.Uid <> usr.ID Then
				Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MODERATOR_ACCESS)
			End If

			Dim i As Integer

			' init right links
			Dim sLinkText As String = (Me.GetPageConstText(htPageConstants, "CONST_ViewGroup_TEXT")).Replace("[PARAM]", grp.Name)
			litRightLinks.Text = "<a href=""" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID + """ class=""white_link"">" + sLinkText + "</a>"

			If Not Me.IsPostBack Then
				' init Genre dropdowns
				Dim tmpLI As ListItem, tmpLI1 As ListItem, tmpLI2 As ListItem, tmpLI3 As ListItem
				Dim genreCount As Integer = Constants.GENRE.List.Count
				Dim tmpGenre As GroupGenres.GroupGenre

				ddlGenre1.Items.Add("--choose one--")
				ddlGenre1.Items.FindByText("--choose one--").Value = "-1"

				ddlGenre2.Items.Add("--choose one--")
				ddlGenre2.Items.FindByText("--choose one--").Value = "-1"

				ddlGenre3.Items.Add("--choose one--")
				ddlGenre3.Items.FindByText("--choose one--").Value = "-1"

				i = 0
				While i < genreCount
					tmpGenre = DirectCast(Constants.GENRE.List(i), GroupGenres.GroupGenre)
					tmpLI1 = New ListItem(tmpGenre.Name, tmpGenre.ID.ToString())
					tmpLI2 = New ListItem(tmpGenre.Name, tmpGenre.ID.ToString())
					tmpLI3 = New ListItem(tmpGenre.Name, tmpGenre.ID.ToString())
					ddlGenre1.Items.Add(tmpLI1)
					ddlGenre2.Items.Add(tmpLI2)
					ddlGenre3.Items.Add(tmpLI3)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' select the Genre1, Genre2, Genre3
				If grpBands.Genre1 > 0 Then
					ddlGenre1.ClearSelection()
					ddlGenre1.Items.FindByValue(grpBands.Genre1.ToString()).Selected = True
				End If

				If grpBands.Genre2 > 0 Then
					ddlGenre2.ClearSelection()
					ddlGenre2.Items.FindByValue(grpBands.Genre2.ToString()).Selected = True
				End If

				If grpBands.Genre3 > 0 Then
					ddlGenre3.ClearSelection()
					ddlGenre3.Items.FindByValue(grpBands.Genre3.ToString()).Selected = True
				End If

				' init LabelTypes dropdown
				Dim labelsCount As Integer = Constants.LABELTYPES.List.Count
				Dim tmpLabel As LabelTypes.LabelType

				ddlLabelType.Items.Add("--choose one--")
				ddlLabelType.Items.FindByText("--choose one--").Value = "-1"

				i = 0
				While i < labelsCount
					tmpLabel = DirectCast(Constants.LABELTYPES.List(i), LabelTypes.LabelType)
					tmpLI = New ListItem(tmpLabel.Name, tmpLabel.ID.ToString())
					ddlLabelType.Items.Add(tmpLI)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				' select the Label Type
				If grpBands.LabelType > 0 Then
					ddlLabelType.ClearSelection()
					ddlLabelType.Items.FindByValue(grpBands.LabelType.ToString()).Selected = True
				End If

				txtBandMembers.Text = grpBands.Members
				txtInfluences.Text = grpBands.Influences
				txtSoundsLike.Text = grpBands.SoundsLike
				txtSiteURL.Text = grpBands.SiteURL
				txtRecordLabel.Text = grpBands.RecordLabel

				txtBandMembers.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))
				txtBandMembers.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))

				txtInfluences.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))
				txtInfluences.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))

				txtSoundsLike.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))
				txtSoundsLike.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_GROUP_MAX_CHARS.ToString()))

				' init Top links
				hlEditGroupProfile.NavigateUrl = "../" + Constants.EDIT_GROUP_BASIC + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				hlCancelGroup.NavigateUrl = "../" + Constants.CANCEL_ACCOUNT + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				hlEvents.NavigateUrl = "../" + Constants.EDIT_GROUP_EVENTS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
			End If
			If txtRecordLabel.Text = "" Then
				txtRecordLabel.Text = Constants.LABEL_DEFAULT_VALUE
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim temp As String = ""
				Dim grpBands As GroupBands = GroupBands.GetGroupBandsByGroupID(grpID)
				If grpBands = Nothing Then
					grpBands = New GroupBands(grpID)
				End If
				temp = removeScriptTags(Request.Params(txtBandMembers.UniqueID).Trim())
				grpBands.Members = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_GROUP_MAX_CHARS))

				temp = removeScriptTags(Request.Params(txtInfluences.UniqueID).Trim())
				grpBands.Influences = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_GROUP_MAX_CHARS))

				temp = removeScriptTags(Request.Params(txtSoundsLike.UniqueID).Trim())
				grpBands.SoundsLike = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_GROUP_MAX_CHARS))

				temp = removeScriptTags(Request.Params(txtSoundsLike.UniqueID).Trim())
				grpBands.SoundsLike = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_GROUP_MAX_CHARS))

				temp = removeScriptTags(Request.Params(txtSiteURL.UniqueID).Trim())
				temp = initSiteURL(temp)
				grpBands.SiteURL = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_BANDSITEURL_MAX_CHARS))

				temp = removeScriptTags(Request.Params(txtRecordLabel.UniqueID).Trim())
				grpBands.RecordLabel = temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_BANDRECORDLABEL_MAX_CHARS))

				grpBands.Genre1 = Convert.ToInt32(Request.Params(ddlGenre1.UniqueID).Trim())
				grpBands.Genre2 = Convert.ToInt32(Request.Params(ddlGenre2.UniqueID).Trim())
				grpBands.Genre3 = Convert.ToInt32(Request.Params(ddlGenre3.UniqueID).Trim())
				grpBands.LabelType = Convert.ToInt32(Request.Params(ddlLabelType.UniqueID).Trim())

				grpBands.Save()
				lblChangesSubmitted.Visible = True
			End If
		End Sub

		Private Function initSiteURL(ByVal sURL As String) As String
			Dim bHttpAndWww As Boolean = sURL.StartsWith("http://www.")
			Dim bHttp As Boolean = sURL.StartsWith("http://")
			Dim bWww As Boolean = sURL.StartsWith("www.")
			Dim tmp As String = ""

			If sURL <> "" Then
				If bHttpAndWww Then
					tmp = sURL
ElseIf bWww Then
					tmp = "http://" + sURL
ElseIf bHttp Then
					tmp = sURL.Substring(7, sURL.Length - 7)
					tmp = "http://www." + tmp
				Else
					tmp = "http://www." + sURL
				End If
			End If
			Return tmp
		End Function

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






